home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / uim / util.scm < prev    next >
Encoding:
Text File  |  2010-11-07  |  9.3 KB  |  327 lines

  1. ;;; util.scm: Utility functions for uim.
  2. ;;;
  3. ;;; Copyright (c) 2003-2009 uim Project http://code.google.com/p/uim/
  4. ;;;
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Redistribution and use in source and binary forms, with or without
  8. ;;; modification, are permitted provided that the following conditions
  9. ;;; are met:
  10. ;;; 1. Redistributions of source code must retain the above copyright
  11. ;;;    notice, this list of conditions and the following disclaimer.
  12. ;;; 2. Redistributions in binary form must reproduce the above copyright
  13. ;;;    notice, this list of conditions and the following disclaimer in the
  14. ;;;    documentation and/or other materials provided with the distribution.
  15. ;;; 3. Neither the name of authors nor the names of its contributors
  16. ;;;    may be used to endorse or promote products derived from this software
  17. ;;;    without specific prior written permission.
  18. ;;;
  19. ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
  20. ;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. ;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
  23. ;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. ;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. ;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. ;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. ;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. ;;; SUCH DAMAGE.
  30.  
  31. (require-extension (srfi 1 6 34))
  32.  
  33. (require "ichar.scm")
  34. (require "deprecated-util.scm")
  35.  
  36.  
  37. (define hyphen-sym (string->symbol "-"))
  38.  
  39. ;;
  40. ;; generic utilities
  41. ;;
  42.  
  43. (define writeln
  44.   (lambda args
  45.     (apply write args)
  46.     (newline)))
  47.  
  48. ;; Make escaped string literal to print a form.
  49. ;;
  50. ;; (string-escape "a str\n") -> "\"a str\\n\""
  51. ;;
  52. ;; The following two codes must display same result. See
  53. ;; test/test-util.scm for further specification.
  54. ;;
  55. ;; (display str)
  56. ;;
  57. ;; (use srfi-6)
  58. ;; (define estr (string-append "(display " (string-escape str) ")"))
  59. ;; (eval (read (open-input-string estr))
  60. ;;       (interaction-environment))
  61. (define string-escape
  62.   (lambda (s)
  63.     (let ((p (open-output-string)))
  64.       (write s p)
  65.       (get-output-string p))))
  66.  
  67. ;; procedural 'or' for use with 'apply'
  68. ;; e.g. (apply proc-or boolean-lst)
  69. ;; should be deprecated and replaced with a proper, Schemer's way
  70. (define proc-or
  71.   (lambda xs
  72.     (reduce (lambda (x y)
  73.           (or x y))
  74.         #f xs)))
  75.  
  76. ;; procedural 'and' for use with 'apply'
  77. ;; e.g. (apply proc-and boolean-lst)
  78. ;; should be deprecated and replaced with a proper, Schemer's way
  79. (define proc-and
  80.   (lambda xs
  81.     (reduce (lambda (x y)
  82.           (and x y))
  83.         #t xs)))
  84.  
  85. ;; meaning of 'end' has been changed from uim 1.5.0. See
  86. ;; doc/COMPATIBILITY and test-util.scm.
  87. (define sublist
  88.   (lambda (lst start end)
  89.     (take (drop lst start)
  90.       (- end start))))
  91.  
  92. ;; meaning of 'len' has been changed from uim 1.5.0. See
  93. ;; doc/COMPATIBILITY and test-util.scm.
  94. (define sublist-rel
  95.   (lambda (lst start len)
  96.     (take (drop lst start)
  97.       len)))
  98.  
  99. (define alist-replace
  100.   (lambda (kons alist)
  101.     (let* ((id (car kons))
  102.        (preexisting (assoc id alist)))
  103.       (if preexisting
  104.       (begin
  105.         (set-cdr! preexisting (cdr kons))
  106.         alist)
  107.       (cons kons alist)))))
  108.  
  109. (define list-join
  110.   (lambda (lst sep)
  111.     (if (null? lst)
  112.     '()
  113.     (cdr (fold-right (lambda (kar kdr)
  114.                (cons* sep kar kdr))
  115.              '() lst)))))
  116.  
  117. ;; downward compatible with SRFI-13 string-join
  118. (define string-join
  119.   (lambda (str-list sep)
  120.     (apply string-append (list-join str-list sep))))
  121.  
  122. ;; Split pattern has been changed from uim 1.5.0. See
  123. ;; doc/COMPATIBILITY and test-uim-util.scm.
  124. (define string-split
  125.   (lambda (str sep)
  126.     (let ((slen (string-length str))
  127.       (seplen (string-length sep)))
  128.       (let rec ((start 0))
  129.     (let ((next (and (<= start slen)
  130.              (string-contains str sep start))))
  131.       (if next
  132.           (cons (substring str start next)
  133.             (rec (+ next seplen)))
  134.           (list (substring str start slen))))))))
  135.  
  136. (define string-append-map
  137.   (lambda args
  138.     (apply string-append (apply map args))))
  139.  
  140. ;; symbol-append is a de facto standard procedure name
  141. (define symbol-append
  142.   (lambda args
  143.     (string->symbol (string-append-map symbol->string args))))
  144.  
  145. ;; only accepts single-arg functions
  146. ;; (define caddr (compose car cdr cdr))
  147. ;; FIXME: remove the closure overhead
  148. (define compose
  149.   (lambda funcs
  150.     (reduce-right (lambda (f g)
  151.             (lambda (x)
  152.               (f (g x))))
  153.           values funcs)))
  154.  
  155. (define method-delegator-new
  156.   (lambda (dest-getter method)
  157.     (lambda (self . args)
  158.       (apply method (cons (dest-getter self) args)))))
  159.  
  160. (define safe-car
  161.   (lambda (pair)
  162.     (and (pair? pair)
  163.      (car pair))))
  164.  
  165. (define safe-cdr
  166.   (lambda (pair)
  167.     (and (pair? pair)
  168.      (cdr pair))))
  169.  
  170. (define assq-cdr
  171.   (lambda (key alist)
  172.     (safe-cdr (assq key alist))))
  173.  
  174. (define clamp
  175.   (lambda (x bottom ceiling)
  176.     (max bottom
  177.      (min x ceiling))))
  178.  
  179.  
  180. ;;
  181. ;; uim-specific utilities
  182. ;;
  183.  
  184. (define do-nothing (lambda args #f))
  185.  
  186. (define make-scm-pathname
  187.   (lambda (file)
  188.     (if (string-prefix? "/" file)
  189.     file
  190.     (string-append (load-path) "/" file))))
  191.  
  192. ;; TODO: write test
  193. ;; returns succeeded or not
  194. (define try-load
  195.   (lambda (file)
  196.     (guard (err
  197.         (else #f))
  198.       ;; to suppress error message, check file existence first
  199.       (and (file-readable? (make-scm-pathname file))
  200.        (load file)))))
  201.  
  202. ;; TODO: write test
  203. ;; returns succeeded or not
  204. (define try-require
  205.   (lambda (file)
  206.     (guard (err
  207.         (else #f))
  208.       ;; to suppress error message, check file existence first
  209.       (and (file-readable? (make-scm-pathname file))
  210.        (require file)))))
  211.  
  212. ;; used for dynamic environment substitution of closure
  213. (define %%enclose-another-env
  214.   (lambda (closure another-env)
  215.     (let* ((code (%%closure-code closure))
  216.        (args (car code))
  217.        (body (cdr code))
  218.        (definition (list 'lambda args body)))
  219.       (eval definition another-env))))
  220.  
  221. ;; See test/test-util.scm to know what define-record does.
  222. ;; rec-spec requires list of list rather than alist to keep
  223. ;; extensibility (e.g. (nth 2 spec) and so on may be used)
  224. (define define-record
  225.   (lambda (rec-sym rec-spec)
  226.     (for-each (lambda (spec index)
  227.         (let* ((elem-sym (list-ref spec 0))
  228.                (default  (list-ref spec 1))
  229.                (getter-sym (symbol-append rec-sym hyphen-sym elem-sym))
  230.                (getter (lambda (rec)
  231.                  (list-ref rec index)))
  232.                (setter-sym (symbol-append
  233.                     rec-sym hyphen-sym 'set- elem-sym '!))
  234.                (setter (lambda (rec val)
  235.                  (set-car! (list-tail rec index)
  236.                        val))))
  237.           (eval (list 'define getter-sym getter)
  238.             (interaction-environment))
  239.           (eval (list 'define setter-sym setter)
  240.             (interaction-environment))))
  241.           rec-spec
  242.           (iota (length rec-spec)))
  243.     (let ((creator-sym (symbol-append rec-sym hyphen-sym 'new))
  244.       (creator (let ((defaults (map cadr rec-spec)))
  245.              (lambda init-lst
  246.                (cond
  247.             ((null? init-lst)
  248.              (list-copy defaults))
  249.             ;; fast path
  250.             ((= (length init-lst)
  251.                 (length defaults))
  252.              (list-copy init-lst))
  253.             ;; others
  254.             ((< (length init-lst)
  255.                 (length defaults))
  256.              (let* ((rest-defaults (list-tail defaults
  257.                               (length init-lst)))
  258.                 (complemented-init-lst (append init-lst
  259.                                    rest-defaults)))
  260.                (list-copy complemented-init-lst)))
  261.             (else
  262.              #f))))))
  263.       (eval (list 'define creator-sym creator)
  264.         (interaction-environment)))))
  265.  
  266. ;; for direct candidate selection
  267. (define number->candidate-index
  268.   (lambda (n)
  269.     (cond
  270.      ((= n 0)
  271.       9)
  272.      ((and (>= n 1)
  273.        (<= n 9))
  274.       (- n 1))
  275.      (else
  276.       n))))
  277.  
  278. ;; update style-element vars
  279. ;; style-spec requires list of (style-element-name . validator)
  280. (define update-style
  281.   (lambda (style-spec style)
  282.     (let* ((elem (car style))
  283.        (name (car elem))
  284.        (val (if (symbol? (cdr elem))
  285.             (symbol-value (cdr elem))
  286.             (cdr elem)))
  287.        (spec (assq name style-spec))
  288.        (valid? (symbol-value (cdr spec))))
  289.       (if (valid? val)
  290.       (set-symbol-value! name val))
  291.       (if (not (null? (cdr style)))
  292.       (update-style style-spec (cdr style))))))
  293.  
  294. ;;
  295. ;; Preedit color related configurations and functions.
  296. ;;
  297. (define reversed-preedit-foreground #f)
  298. (define reversed-preedit-background #f)
  299. (define separator-foreground #f)
  300. (define separator-background #f)
  301. (define reversed-separator-foreground #f)
  302. (define reversed-separator-background #f)
  303.  
  304. (define uim-color-spec
  305.   '((reversed-preedit-foreground   . string?)
  306.     (reversed-preedit-background   . string?)
  307.     (separator-foreground          . string?)
  308.     (separator-background          . string?)
  309.     (reversed-separator-foreground . string?)
  310.     (reversed-separator-background . string?)))
  311.  
  312. ;; predefined color styles
  313. (define uim-color-uim
  314.   '((reversed-preedit-foreground   . "white")
  315.     (reversed-preedit-background   . "black")
  316.     (separator-foreground          . "lightsteelblue")
  317.     (separator-background          . "")
  318.     (reversed-separator-foreground . "white")
  319.     (reversed-separator-background . "black")))
  320. (define uim-color-atok
  321.   '((reversed-preedit-foreground   . "black")
  322.     (reversed-preedit-background   . "cyan")
  323.     (separator-foreground          . "lightsteelblue")
  324.     (separator-background          . "")
  325.     (reversed-separator-foreground . "black")
  326.     (reversed-separator-background . "blue")))
  327.